package com.mzy.basics.singleCase;

/**
 * Created with IntelliJ IDEA.
 *
 * @Author mzy
 * @Date 2022/3/9 23:17
 * @Version 1.0
 */
public class Singleton2Lock {

    private static volatile Singleton2Lock instance =null;

    private Singleton2Lock(){

    }

    /**
     * 3、双重检查加锁（推荐）
     */
    public static Singleton2Lock getInstanceC() {
        // 先判断实例是否存在，若不存在再对类对象进行加锁处理
        if (instance == null) {
            synchronized (Singleton2Lock.class) {
                // 防止指令重排序问题
                if (instance == null) {
                    instance = new Singleton2Lock();
                }
            }
        }
        return instance;
    }
}
